// 版权归 Epic Games, Inc. 所有，保留所有权利。

#include "LyraUIMessaging.h"

#include "Messaging/CommonGameDialog.h"
#include "NativeGameplayTags.h"
#include "CommonLocalPlayer.h"
#include "PrimaryGameLayout.h"
#include "Widgets/CommonActivatableWidgetContainer.h"

#include UE_INLINE_GENERATED_CPP_BY_NAME(LyraUIMessaging)

class FSubsystemCollectionBase;

UE_DEFINE_GAMEPLAY_TAG_STATIC(TAG_UI_LAYER_MODAL, "UI.Layer.Modal"); // 定义UI模态层标签

/**
 * 初始化子系统
 * @param Collection 子系统集合基类引用
 */
void ULyraUIMessaging::Initialize(FSubsystemCollectionBase& Collection)
{
	Super::Initialize(Collection); // 调用父类初始化

	ConfirmationDialogClassPtr = ConfirmationDialogClass.LoadSynchronous(); // 同步加载确认对话框类
	ErrorDialogClassPtr = ErrorDialogClass.LoadSynchronous(); // 同步加载错误对话框类
}

/**
 * 显示确认对话框
 * @param DialogDescriptor 对话框描述符
 * @param ResultCallback 结果回调委托
 */
void ULyraUIMessaging::ShowConfirmation(UCommonGameDialogDescriptor* DialogDescriptor, FCommonMessagingResultDelegate ResultCallback)
{
	if (UCommonLocalPlayer* LocalPlayer = GetLocalPlayer<UCommonLocalPlayer>()) // 获取通用本地玩家
	{
		if (UPrimaryGameLayout* RootLayout = LocalPlayer->GetRootUILayout()) // 获取根UI布局
		{
			RootLayout->PushWidgetToLayerStack<UCommonGameDialog>(TAG_UI_LAYER_MODAL, ConfirmationDialogClassPtr, [DialogDescriptor, ResultCallback](UCommonGameDialog& Dialog) { // 推送确认对话框到模态层
				Dialog.SetupDialog(DialogDescriptor, ResultCallback); // 设置对话框
			});
		}
	}
}

/**
 * 显示错误对话框
 * @param DialogDescriptor 对话框描述符
 * @param ResultCallback 结果回调委托
 */
void ULyraUIMessaging::ShowError(UCommonGameDialogDescriptor* DialogDescriptor, FCommonMessagingResultDelegate ResultCallback)
{
	if (UCommonLocalPlayer* LocalPlayer = GetLocalPlayer<UCommonLocalPlayer>()) // 获取通用本地玩家
	{
		if (UPrimaryGameLayout* RootLayout = LocalPlayer->GetRootUILayout()) // 获取根UI布局
		{
			RootLayout->PushWidgetToLayerStack<UCommonGameDialog>(TAG_UI_LAYER_MODAL, ErrorDialogClassPtr, [DialogDescriptor, ResultCallback](UCommonGameDialog& Dialog) { // 推送错误对话框到模态层
				Dialog.SetupDialog(DialogDescriptor, ResultCallback); // 设置对话框
			});
		}
	}
}